﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace BUS
{
    public class XuLyLoaiPhong
    {
        // tim toan bo loai phong
        public static DataTable TimToanBoLoaiPhong()
        {
            DAO.CDataProvider reader = new DAO.CDataProvider();
            String sql = "SELECT * FROM LOAIPHONG ";

            DataTable dt = new DataTable();
            try
            {
                dt = reader.DocDuLieu(sql);
            }
            catch (System.Exception )
            {
            	
            }

            return dt;
        }
        // tim 1 loai phong theo
        public static DTO.CLOAIPHONG TimLoaiPhongTheoMaLoaiPhong(String MaLoaiP)
        {
            DTO.CLOAIPHONG lp = new DTO.CLOAIPHONG();
            // lop DataProvider
            DAO.CDataProvider reader = new DAO.CDataProvider();
            // chuoi SELECT
            String sql = " SELECT * FROM LOAIPHONG " +
            " WHERE MaLoaiPhong = '" + MaLoaiP.ToString() + "' ";

            try
            {
                // Bang data se duoc nhap
                DataTable dt = reader.DocDuLieu(sql);

                // Neu ton tai phong nay
                if (dt.Rows.Count == 1)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        lp.MaLoaiPhong = dr["MaLoaiPhong"].ToString();
                        lp.LoaiPhong = dr["LoaiPhong"].ToString();
                        lp.GiaTien = int.Parse(dr["GiaTien"].ToString());
                    }
                }
                // phong nay ko ton tai
                else
                {
                    lp.MaLoaiPhong = " ";
                    lp.LoaiPhong = " ";
                    lp.GiaTien = 0;
                }

            }
            catch (System.Exception )
            {
            	
            }
            
            return lp;
        }
        // them 1 loai phong
        public static Boolean ThemLoaiPhong(DTO.CLOAIPHONG lp)
        {
            DAO.CDataProvider writer = new DAO.CDataProvider();

            // Cau lenh SQL
            String insertSQL = " INSERT INTO LOAIPHONG(MaLoaiPhong,LoaiPhong,GiaTien) "
                + " VALUES('" + lp.MaLoaiPhong.ToString() + "',"
                + "'" + lp.LoaiPhong.ToString() + "',"
                + lp.GiaTien + ") ";

            // Thuc thi cau lenh insert
            if (writer.ThucThiDuLieu(insertSQL))
                return true;
            else
                return false;
        }
        // sua thong tin 1 loai phong
        public static Boolean SuaTTLoaiPhong(DTO.CLOAIPHONG lp,int sothutu)
        {
            DAO.CDataProvider writer = new DAO.CDataProvider();

            // Cau lenh SQL
            String updateSQL = " UPDATE LOAIPHONG "
                + " SET MaLoaiPhong = '" + lp.MaLoaiPhong.ToString() + "', "
                + " LoaiPhong ='" + lp.LoaiPhong.ToString() + "', "
                + " GiaTien = " + lp.GiaTien + " "
                + " WHERE STT =" + sothutu.ToString() + " ";

            // Thuc thi cau lenh insert
            if (writer.ThucThiDuLieu(updateSQL))
                return true;
            else
                return false;
        }
        // xoa 1 loai phong
        public static Boolean XoaLoaiPhong(String MaLoaiP)
        {
            DAO.CDataProvider writer = new DAO.CDataProvider();

            // Cau lenh SQL
            String deleteSQL = " DELETE FROM LOAIPHONG "
                + " WHERE MaLoaiPhong ='" + MaLoaiP + "' ";

            // Thuc thi cau lenh insert
            if (writer.ThucThiDuLieu(deleteSQL))
                return true;
            else
                return false;
        }
    }
}
